home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / tools / fakeunix.c < prev    next >
C/C++ Source or Header  |  1990-07-15  |  5KB  |  316 lines

  1. #ifdef __GNUC__
  2. #include <unixlib.h>
  3. #include <osbind.h>
  4.  
  5. #else
  6.  
  7. extern long    gemdos();
  8.  
  9. #define    creat(a,b)    (int)gemdos(0x3c,a,0)
  10. #define    open(a,b)    (int)gemdos(0x3d,a,b)
  11. #define    close(a)    (int)gemdos(0x3e,a)
  12. #define    read(a,b,c)    (int)gemdos(0x3f,a,(long)((unsigned)c),b)
  13. #define    write(a,b,c)    (int)gemdos(0x40,a,(long)((unsigned)c),b)
  14. #define    lseek(a,b,c)         gemdos(0x42,b,a,c)
  15. #define    free(a)        (int)gemdos(0x49,a)
  16. #define    exit(a)             gemdos(0x4c,a)
  17. #define    malloc        myalloc
  18.  
  19. char *myalloc(siz)
  20. unsigned siz;
  21. {
  22.     long n;
  23.  
  24.     n = gemdos(0x48,(long)siz);
  25.     if (n < 0)
  26.         return((char *)0);
  27.     return((char *)n);
  28. }
  29.  
  30. abort()
  31. {
  32.     printf("Abort\n");
  33.     exit(-1);
  34. }
  35.  
  36. _outc(c)
  37. {
  38.     if (c == '\n')
  39.         _outc('\r');
  40.     gemdos(0x02,c);
  41. }
  42.  
  43. printf(f, a) char *f; {
  44.     _doprnt(f, &a, _outc);
  45. }
  46.  
  47. /*--------------------------------------------------------------------*/
  48.  
  49. #define    NO_FLOAT
  50.  
  51. /* @(#)doprnt.c    1.1 */
  52. /*
  53.  * C version of doprnt.s for ACK
  54.  * three compile time options:
  55.  *    STACKUP        fetch arguments using *p-- instead of *p++
  56.  *    NO_LONGD    %d and %ld/%D are equal
  57.  *    NO_FLOAT    abort on %e, %f and %g
  58.  */
  59.  
  60. static char *
  61. itoa(p, num, radix)
  62. register char *p;
  63. register unsigned num;
  64. register radix;
  65. {
  66.     register    i;
  67.     register char    *q;
  68.  
  69.     q = p + 32;
  70.     do {
  71.         i = (int)(num % radix);
  72. #ifdef TOS
  73.         if (i < 0) i += radix;    /* Bug in Alcyon 4.14 C */
  74. #endif
  75.         i += '0';
  76.         if (i > '9')
  77.             i += 'A' - '0' - 10;
  78.         *--q = i;
  79.     } while (num = num / radix);
  80.     i = (int)(p + 32 - q);
  81.     do
  82.         *p++ = *q++;
  83.     while (--i);
  84.     return(p);
  85. }
  86.  
  87. #ifndef NO_LONGD
  88. static char *
  89. ltoa(p, num, radix)
  90. register char *p;
  91. register unsigned long num;
  92. register radix;
  93. {
  94.     register    i;
  95.     register char    *q;
  96.  
  97.     q = p + 32;
  98.     do {
  99.         i = (int)(num % radix);
  100.         i += '0';
  101.         if (i > '9')
  102.             i += 'A' - '0' - 10;
  103.         *--q = i;
  104.     } while (num = num / radix);
  105.     i = (int)(p + 32 - q);
  106.     do
  107.         *p++ = *q++;
  108.     while (--i);
  109.     return(p);
  110. }
  111. #endif
  112.  
  113. #ifndef NO_FLOAT
  114. extern char    *_ecvt();
  115. extern char    *_fcvt();
  116. extern char    *_gcvt();
  117. #endif
  118.  
  119. #ifdef STACKUP
  120. #define    GETARG(typ)    *((typ *)args)--
  121. #else
  122. #define    GETARG(typ)    *((typ *)args)++
  123. #endif STACKUP
  124.  
  125. _doprnt(fmt, args, outc)
  126. register char *fmt;
  127. register int *args;
  128. int (*outc)();
  129. {
  130.     char        buf[128];
  131.     register char    *p;
  132.     register char    *s;
  133.     register    c;
  134.     register    i;
  135.     register short    width;
  136.     register short    ndigit;
  137.     register    ndfnd;
  138.     register    ljust;
  139.     register    zfill;
  140. #ifndef NO_LONGD
  141.     register    lflag;
  142.     register long    l;
  143. #endif
  144.  
  145.     for (;;) {
  146.         p = buf;
  147.         s = buf;
  148.         while ((c = *fmt++) && c != '%')
  149.             (*outc)(c);
  150.         if (c == 0)
  151.             return;
  152.         ljust = 0;
  153.         if (*fmt == '-') {
  154.             fmt++;
  155.             ljust++;
  156.         }
  157.         zfill = ' ';
  158.         if (*fmt == '0') {
  159.             fmt++;
  160.             zfill = '0';
  161.         }
  162.         for (width = 0;;) {
  163.             c = *fmt++;
  164.             if (c >= '0' && c <= '9')
  165.                 c -= '0';
  166.             else if (c == '*')
  167.                 c = GETARG(int);
  168.             else
  169.                 break;
  170.             width *= 10;
  171.             width += c;
  172.         }
  173.         ndfnd = 0;
  174.         ndigit = 0;
  175.         if (c == '.') {
  176.             for (;;) {
  177.                 c = *fmt++;
  178.                 if (c >= '0' && c <= '9')
  179.                     c -= '0';
  180.                 else if (c == '*')
  181.                     c = GETARG(int);
  182.                 else
  183.                     break;
  184.                 ndigit *= 10;
  185.                 ndigit += c;
  186.                 ndfnd++;
  187.             }
  188.         }
  189. #ifndef NO_LONGD
  190.         lflag = 0;
  191. #endif
  192.         if (c == 'l' || c == 'L') {
  193. #ifndef NO_LONGD
  194.             lflag++;
  195. #endif
  196.             if (*fmt)
  197.                 c = *fmt++;
  198.         }
  199.         switch (c) {
  200.         case 'X':
  201. #ifndef NO_LONGD
  202.             lflag++;
  203. #endif
  204.         case 'x':
  205.             c = 16;
  206.             goto oxu;
  207.         case 'U':
  208. #ifndef NO_LONGD
  209.             lflag++;
  210. #endif
  211.         case 'u':
  212.             c = 10;
  213.             goto oxu;
  214.         case 'O':
  215. #ifndef NO_LONGD
  216.             lflag++;
  217. #endif
  218.         case 'o':
  219.             c = 8;
  220.         oxu:
  221. #ifndef NO_LONGD
  222.             if (lflag) {
  223.                 p = ltoa(p, GETARG(long), c);
  224.                 break;
  225.             }
  226. #endif
  227.             p = itoa(p, GETARG(int), c);
  228.             break;
  229.         case 'D':
  230. #ifndef NO_LONGD
  231.             lflag++;
  232. #endif
  233.         case 'd':
  234. #ifndef NO_LONGD
  235.             if (lflag) {
  236.                 if ((l = GETARG(long)) < 0) {
  237.                     *p++ = '-';
  238.                     l = -l;
  239.                 }
  240.                 p = ltoa(p, l, 10);
  241.                 break;
  242.             }
  243. #endif
  244.             if ((i = GETARG(int)) < 0) {
  245.                 *p++ = '-';
  246.                 i = -i;
  247.             }
  248.             p = itoa(p, i, 10);
  249.             break;
  250. #ifdef NO_FLOAT
  251.         case 'e':
  252.         case 'f':
  253.         case 'g':
  254.             zfill = ' ';
  255.             *p++ = '?';
  256.             break;
  257. #else
  258.         case 'e':
  259.             if (ndfnd == 0)
  260.                 ndigit = 6;
  261.             ndigit++;
  262.             p = _ecvt(p, GETARG(double), ndigit);
  263.             break;
  264.         case 'f':
  265.             if (ndfnd == 0)
  266.                 ndigit = 6;
  267.             p = _fcvt(p, GETARG(double), ndigit);
  268.             break;
  269.         case 'g':
  270.             if (ndfnd == 0)
  271.                 ndigit = 6;
  272.             p = _gcvt(p, GETARG(double), ndigit);
  273.             break;
  274. #endif
  275.         case 'c':
  276.             zfill = ' ';
  277.             if (c = GETARG(int))
  278.                 *p++ = c;
  279.             break;
  280.         case 's':
  281.             zfill = ' ';
  282.             if ((s = GETARG(char *)) == 0)
  283.                 s = "(null)";
  284.             if (ndigit == 0)
  285.                 ndigit = 32767;
  286.             for (p = s; *p && --ndigit >= 0; p++)
  287.                 ;
  288.             break;
  289.         default:
  290.             *p++ = c;
  291.             break;
  292.         }
  293.         i = (int)(p - s);
  294.         if ((width -= i) < 0)
  295.             width = 0;
  296.         if (ljust == 0)
  297.             width = -width;
  298.         if (width < 0) {
  299.             if (*s=='-' && zfill=='0') {
  300.                 (*outc)(*s++);
  301.                 i--;
  302.             }
  303.             do
  304.                 (*outc)(zfill);
  305.             while (++width != 0);
  306.         }
  307.         while (--i>=0)
  308.             (*outc)(*s++);
  309.         while (width) {
  310.             (*outc)(zfill);
  311.             width--;
  312.         }
  313.     }
  314. }
  315. #endif /* __GNUC__ */
  316.